//
//  ProductInformationShowCell.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/30.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ProductInformationShowCell.h"
#import "XXTool.h"

@interface ProductInformationShowCell ()

@property (nonatomic) UIView* content;

@property (nonatomic) UIImageView *productImg;

@property (nonatomic) UILabel *companyNameLabel;

@property (nonatomic) UILabel *indexLabel;

@property (nonatomic) UIView *line;

@property (nonatomic) VerticalSettingLabel *prodcutNameLabel;

@end

@implementation ProductInformationShowCell
{
    CGFloat gap;
}
-(UIView*)content
{
    if (_content==nil)
    {
        _content = [[UIView alloc] init];
        _content.backgroundColor = [UIColor whiteColor];
        [self addSubview:_content];
    }
    return _content;
}

-(UIImageView*)productImg
{
    if (_productImg==nil)
    {
        _productImg = [[UIImageView alloc] init];
        _productImg.backgroundColor = [XXTool colorWithBackGround];
        _productImg.contentMode = UIViewContentModeScaleAspectFill;
        _productImg.clipsToBounds = YES;
        [_content addSubview:_productImg];
    }
    return _productImg;
}

-(UILabel*)companyNameLabel
{
    if (_companyNameLabel==nil)
    {
        _companyNameLabel = [[UILabel alloc] init];
        //_companyNameLabel.textColor = [UIColor blackColor];
        _companyNameLabel.textColor = [UIColor grayColor];
        _companyNameLabel.font = [XXTool systemFontOfSize:17];
        _companyNameLabel.numberOfLines = 0;
        [_content addSubview:_companyNameLabel];
    }
    return _companyNameLabel;
}

-(UILabel*)indexLabel
{
    if (_indexLabel==nil)
    {
        _indexLabel = [[UILabel alloc] init];
        //_indexLabel.textColor = [XXTool colorWithProject];
        _indexLabel.textColor = [UIColor grayColor];
        _indexLabel.font = [XXTool systemFontOfSize:15];
        [_content addSubview:_indexLabel];
    }
    return _indexLabel;
}

-(UIView*)line
{
    if (_line==nil)
    {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [XXTool colorWithLine];
        [_content addSubview:_line];
    }
    return _line;
}

-(UILabel*)prodcutNameLabel
{
    if (_prodcutNameLabel==nil)
    {
        _prodcutNameLabel = [[VerticalSettingLabel alloc] init];
        _prodcutNameLabel.textColor = [UIColor grayColor];
        _prodcutNameLabel.font = [XXTool boldSystemFontOfSize:15];
        _prodcutNameLabel.numberOfLines = 0;
        _prodcutNameLabel.verticalAlignment = VerticalAlignmentTop;
        [_content addSubview:_prodcutNameLabel];
    }
    return _prodcutNameLabel;
}


-(void)initParameter
{
    gap = 6;
}

-(void)initUI
{
    self.backgroundColor = [UIColor clearColor];
    [self content];
    [self productImg];
    [self companyNameLabel];
    [self prodcutNameLabel];
    [self line];
    [self indexLabel];
}

-(void)refreshLayout:(CGRect)frame
{
    _content.frame = CGRectMake(gap
                                , gap
                                , frame.size.width-gap*2
                                , frame.size.height-gap*2);
    _productImg.frame = CGRectMake(8
                                   , 12
                                   , 90
                                   , 90);
    _companyNameLabel.frame = CGRectMake(_productImg.frame.size.width+_productImg.frame.origin.x+8
                                         , _productImg.frame.origin.y-4
                                         , _content.frame.size.width-(8+_productImg.frame.size.width+_productImg.frame.origin.x*2)
                                         , [XXTool getTextHeightByText:_companyNameLabel.text
                                                                  Font:_companyNameLabel.font
                                                                 Width:_companyNameLabel.frame.size.width]);
    _indexLabel.frame = CGRectMake(_companyNameLabel.frame.origin.x
                                   , _companyNameLabel.frame.size.height+_companyNameLabel.frame.origin.y
                                   , _companyNameLabel.frame.size.width
                                   , 17);
    _line.frame = CGRectMake(_companyNameLabel.frame.origin.x
                             , _indexLabel.frame.size.height+_indexLabel.frame.origin.y+gap
                             , _companyNameLabel.frame.size.width+8
                             , 0.5);
    _prodcutNameLabel.frame = CGRectMake(_companyNameLabel.frame.origin.x
                                         , _line.frame.size.height+_line.frame.origin.y+gap
                                         , _companyNameLabel.frame.size.width
                                         , _content.frame.size.height-3*gap-_companyNameLabel.frame.size.height-_indexLabel
                                         .frame.size.height-_companyNameLabel.frame.origin.y*2);

}

-(void)setCellWithData:(XMLProductInformationShowModel *)model
{
    [_productImg setImageWithUrl:model.goods_photo placerHolder:[UIImage imageNamed:@"默认展品图"]];
    _companyNameLabel.text = [NSString stringWithFormat:@"%@\n%@",model.cpname,model.cpname_en];
    _prodcutNameLabel.text = model.goods_name;
    
    if ([model.regflag isEqualToString:@"1"]) //1有注册
    {
        _companyNameLabel.textColor = [UIColor blackColor];
        _indexLabel.textColor = [XXTool colorWithProject];
    }
    else
    {
        _companyNameLabel.textColor = [UIColor grayColor];
        _indexLabel.textColor = [UIColor grayColor];
    }
    
    if (![model.goods_index isEqualToString:@""])
    {
        NSArray *exIndexArray = [model.goods_index componentsSeparatedByString:@","];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSString *indexShow = @"";
            for (NSString *temp in exIndexArray)
            {
                RLMResults *indexText = [RXMLExhibitorIndexModel objectsInRealm:[RLMRealm realmWithPath:[XXTool dataBasePath]] where:[NSString stringWithFormat:@"index_NO = '%@'",temp]];
                if (indexText.count>0)
                {
                    RXMLExhibitorIndexModel *indexModel = indexText[0];
                    if ([XXTool didCurrentLanguageChinese])
                    {
                        indexShow = [NSString stringWithFormat:@"%@,%@",indexShow,indexModel.index_name_cn];
                    }
                    else
                    {
                        indexShow = [NSString stringWithFormat:@"%@,%@",indexShow,indexModel.index_name_en];
                    }
                }
                //去除多余,
                if (indexShow.length>1)
                {
                    indexShow = [indexShow substringFromIndex:1];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    _indexLabel.text = indexShow;
                });
            }
        });
    }
    else
    {
        _indexLabel.text = model.goods_index;
    }
    [self refreshLayout:self.frame];
}

@end
